library live_start_count_animation;

import 'dart:async';

import 'package:flutter/material.dart';

class LiveStartCountAnimation {
  ///显示直播倒计时动画
  static void showCountDownAnimation(BuildContext context, {int maxCount = 3}) {
    int count = 1;
    ValueChanged<int>? countUpdater;
    final countValueBuilder = _ValueBuilder<int?>(
      builder: (snapshot, updater) {
        countUpdater = updater;
        return Center(
          child: Container(
            width: 90,
            height: 90,
            alignment: Alignment.center,
            decoration: BoxDecoration(color: Colors.black.withOpacity(0.5), borderRadius: BorderRadius.circular(45.0)),
            child: Text(
              '$snapshot',
              style: const TextStyle(
                  fontSize: 60, fontWeight: FontWeight.bold, color: Colors.white, decoration: TextDecoration.none),
            ),
          ),
        );
      },
      initialValue: 1,
    );

    final overlayEntryLoader = OverlayEntry(builder: (context) {
      return countValueBuilder;
    });

    final overlayEntryOpacity = OverlayEntry(builder: (context) {
      return Opacity(
          opacity: 0.3,
          child: GestureDetector(
            onTap: () {},
            child: Container(
              color: Colors.black,
            ),
          ));
    });

    Overlay.of(context).insert(overlayEntryOpacity);
    Overlay.of(context).insert(overlayEntryLoader);
    Timer.periodic(
      const Duration(milliseconds: 1000),
      (timer) {
        count++;
        if (countUpdater != null) {
          countUpdater!.call(count);
        }
        if (count > maxCount) {
          overlayEntryOpacity.remove();
          overlayEntryLoader.remove();
          timer.cancel();
          countUpdater = null;
        }
      },
    );
  }
}

class _ValueBuilder<T> extends StatefulWidget {
  final T? initialValue;
  final ValueBuilderBuilder<T> builder;
  final void Function()? onDispose;
  final void Function(T)? onUpdate;

  const _ValueBuilder({
    super.key,
    this.initialValue,
    this.onDispose,
    this.onUpdate,
    required this.builder,
  });

  @override
  _ValueBuilderState<T> createState() => _ValueBuilderState<T>();
}

class _ValueBuilderState<T> extends State<_ValueBuilder<T?>> {
  T? value;

  @override
  void initState() {
    super.initState();
    value = widget.initialValue;
  }

  @override
  Widget build(BuildContext context) => widget.builder(value, updater);

  void updater(T? newValue) {
    if (widget.onUpdate != null) {
      widget.onUpdate!(newValue);
    }
    setState(() {
      value = newValue;
    });
  }

  @override
  void dispose() {
    super.dispose();
    widget.onDispose?.call();
    if (value is ChangeNotifier) {
      (value as ChangeNotifier?)?.dispose();
    } else if (value is StreamController) {
      (value as StreamController?)?.close();
    }
    value = null;
  }
}

typedef ValueBuilderUpdateCallback<T> = void Function(T snapshot);
typedef ValueBuilderBuilder<T> = Widget Function(T snapshot, ValueBuilderUpdateCallback<T> updater);
